package com.demo.controller;

import com.demo.controller.base.BaseCtrl;
import com.demo.controller.model.RSResult;
import com.demo.core.ActionContext;
import com.demo.po.ServerPo;
import com.demo.service.IServerService;
import com.demo.service.common.BizException;
import com.jcraft.jsch.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/server")
public class ServerController extends BaseCtrl {

    @Autowired
    private IServerService serverService;

    @RequestMapping(value = "/list")
    public RSResult list(@RequestBody ServerPo server) {
        log.info("Start get list of the table : server, the request is : {}", server);
        RSResult result = new RSResult();
        try {
            int count = serverService.countServers(server);
            List<ServerPo> list = new ArrayList<>();
            if (count > 0) {
                server.setPagestart((server.getCurpage() - 1) * server.getPagesize());
                list = serverService.findServersByPage(server);
            }
            result.setResult(0);
            result.setTotal(count);
            result.setData(list);
        } catch (BizException ex) {
            log.error("{} : {}", ex.getErrorcode(), ex.getRemark());
            result.setResult(ex.getErrorcode(), ex.getRemark());
        }
        log.info("Get list of the table : server, success.");
        return result;
    }

    @RequestMapping(value = "/detail")
    public RSResult info(@RequestBody ServerPo server) {
        log.info("Get detail object of the table : server, the request is : {}", server);
        RSResult result = new RSResult();
        try {
            ServerPo obj = serverService.getServer(server.getServerid());
            result.setResult(0);
            result.setData(obj);
        } catch (BizException ex) {
            log.error("{} : {}", ex.getErrorcode(), ex.getRemark());
            result.setResult(ex.getErrorcode(), ex.getRemark());
        }
        log.info("Get detail object of the table : server, success.");
        return result;
    }

    @RequestMapping(value = "/new")
    public RSResult add(@RequestBody ServerPo server) {
        log.info("Add object to the table : server, the request is : {}", server);
        RSResult result = new RSResult();
        try {
            Session session = ActionContext.getSession(server);
            server.setStatus(session == null ? 1 : 0);
            Integer id = serverService.addServer(server);
            result.setResult(0);
            result.setData(id);
        } catch (BizException ex) {
            log.error("{} : {}", ex.getErrorcode(), ex.getRemark());
            result.setResult(ex.getErrorcode(), ex.getRemark());
        }
        log.info("Add object to the table : server, success.");
        return result;
    }

    @RequestMapping(value = "/update")
    public RSResult update(@RequestBody ServerPo server) {
        log.info("Update object from the table : server, the request is : {}", server);
        RSResult result = new RSResult();
        try {
            Session session = ActionContext.getSession(server);
            server.setStatus(session == null ? 1 : 0);
            serverService.updateServer(server);
            result.setResult(0);
        } catch (BizException ex) {
            log.error("{} : {}", ex.getErrorcode(), ex.getRemark());
            result.setResult(ex.getErrorcode(), ex.getRemark());
        }
        log.info("Update object from the table : server, success.");
        return result;
    }

    @RequestMapping(value = "/refresh")
    public RSResult refresh(@RequestBody ServerPo server) {
        log.info("Start refresh server login status, params is {}", server);
        RSResult result = new RSResult();
        Session session = ActionContext.getSession(server);
        server.setStatus(session == null ? 1 : 0);
        serverService.updateServer(server);
        result.setResult(0);
        log.info("End refresh server login status.");
        return result;
    }

    @RequestMapping(value = "/delete")
    public RSResult delete(@RequestBody ServerPo server) {
        log.info("Delete object from the table : server, the request is : {}", server);
        RSResult result = new RSResult();
        try {
            serverService.deleteServer(server.getServerid());
            result.setResult(0);
        } catch (BizException ex) {
            log.error("{} : {}", ex.getErrorcode(), ex.getRemark());
            result.setResult(ex.getErrorcode(), ex.getRemark());
        }
        log.info("Delete object from the table : server, success.");
        return result;
    }
}
